Skip to content

Add compressor options#335

Merged
developit merged 5 commits into
masterfrom
compressor-options
Mar 4, 2019
Merged

Add compressor options#335
developit merged 5 commits into
masterfrom
compressor-options

Conversation

@developit

@developit developit commented Mar 4, 2019

Copy link
Copy Markdown
Owner

This allows specifying Terser options in mangle.json or package.json. The old "mangle" field is still supported for legacy reasons alongside the new "minify" option.

// in `mangle.json` or `package.json`:
{
  "minify": {
    // old/simple variant:
    "regex": "^_",
    // new: customize any terser options:
    "compress": {
      "hoist_vars": true,
      "reduce_funcs": false
    }
  }
}

This is useful for projects that are already structured with functions aimed at gzip compression optimization.

This allows specifying Terser options in mangle.json or package.json's "mangle" field.
@developit

developit commented Mar 4, 2019

Copy link
Copy Markdown
Owner Author

I've updated the PR with a new config format.

The previous package.json "mangle" field is still supported for compatibility purposes.

Now, in either mangle.json or package.json, both can specify a "minify" field containing configuration for Terser:

{
  // "minify" for Terser (https://github.com/terser-js/terser#minify-options-structure)
  // Note: works as a key in both package.json or mangle.json
  "minify": {
    // Legacy/convenient way to set mangle.properties.{regex,reserved}:
    "properties": "^_",
    "reserved": ["_dom", "_constructor"],

    // Optionally specify Terser compress configuration.
    // Supported values: https://github.com/terser-js/terser#compress-options
    "compress": {
      "hoist_vars": true,
      "reduce_funcs": false
    },

    // Optionally specify Terser mangle configuration
    // Supported values: https://github.com/terser-js/terser#mangle-options
    "mangle": {}
  }
}

@marvinhagemeister marvinhagemeister left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉👍 I always wondered why we didn't just expose the whole terser config.

@developit

Copy link
Copy Markdown
Owner Author

@marvinhagemeister for now I've left out a couple top-level configuration items like module and ecma because Microbundle is going to automate those soon and we don't want folks to tweak them thinking it's necessary.

@hassanbazzi hassanbazzi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was gonna ask the same as Marvin but makes sense I guess. Can't you just spread the rest of the config onto Terser instead? If someone passes something specific (without it being documented), that means they want something very specific and they know (I think) what they're doing.

So we document and expose exactly what we want people to actually change, and spread the rest just in case.

@developit

Copy link
Copy Markdown
Owner Author

@hassanbazzi most of the Terser options folks would want to change are on mangle and compress, so for those that's what this does - sets some defaults then allows arbitrary overrides.

For Terser's root options, we really can't allow arbitrary customization. Doing so would break output
in the very near future, when Microbundle begins outputting ES2017 alongside the current downlevelled ES5 (see #304). When we do this, we'll be running Terser in ES5 mode for the 3 classic script bundles, and {ecma:8,module:true} for modern bundles. Since ecma and module are the two main root options we're not exposing here, I think that with the modern bundling setup in place there won't really be a reason to customize any root options. We can always add them manually once we have a better idea, but for now I don't want folks to try customizing ecma only to intentionally break it in 1.0.

@developit developit merged commit f6ebebb into master Mar 4, 2019
@bors bors Bot deleted the compressor-options branch March 4, 2019 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants